<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>自定义指令练习</title>
    <script src="js/vue.js"></script>
    <style>
        #app div {
            width: 100px;
            height: 100px;
            position: absolute;
        }

        #app .hello {
            top: 0;
            left: 0;
            background-color: red;
        }

        #app .World {
            top: 0;
            right: 0;
            background-color: orange;
        }
    </style>

</head>

<body>
    <div id="app">
        <div class="hello" v-drag>Hello</div>
        <div class="World" v-drag>World</div>
    </div>
    <script>
        Vue.directive('drag', function (el) {
            el.onmousedown = function (e) {
                var disX = e.clientX - el.offsetLeft;
                var disY = e.clientY - el.offsetTop;

                document.onmousemove = function (e) {
                    var l = e.clientX - disX;
                    var t = e.clientY - disY;
                    el.style.left = l + 'px';
                    el.style.top = t + 'px';
                }

                document.onmouseup = function (e) {
                    document.onmousemove = null;
                    document.onmouseup = null;
                }
            }
        })
        var vm = new Vue({
            el: '#app'
        })
    </script>
</body>

</html>